#include<iostream>
#include<vector>
#include<queue>

class Solution
{
    int dx[4] = { 1,-1,0,0 };
    int dy[4] = { 0,0,1,-1 };
    bool check[101][101];
    int row, cow;
public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance)
    {
        int i = entrance[0], j = entrance[1];
        row = maze.size(), cow = maze[0].size();
        int count = 0;
        queue<pair<int, int>> q;

        q.push({ i,j });
        check[i][j] = true;
        while (q.size())
        {
            int sz = q.size();

            count++;
            for (int k = 0; k < sz; k++)
            {
                auto [a, b] = q.front();
                q.pop();
                for (int l = 0; l < 4; l++)
                {
                    int x = a + dx[l], y = b + dy[l];
                    if (x >= 0 && x < row && y >= 0 && y < cow && !check[x][y] && maze[x][y] != '+')
                    {
                        if (x == 0 || x == row - 1 || y == 0 || y == cow - 1) return count;
                        q.push({ x,y });
                        check[x][y] = true;
                    }
                }
            }

        }
        return -1;
    }
};